// #include "fs.h"

#include "vfs_server.h"
#include <nxos/ipc.h>

NX_Size VFS_Dispatch(NX_IpcMsg *ipc_msg);
void NX_FileSystemInit(void);
NX_Error VFS_FileSystemMount(const char *dev, const char *dir, const char *fsname, NX_U32 flags);

NX_PRIVATE void VFS_IpcHandler(NX_IpcMsg *ipc_msg)
{
    NX_Size ret = VFS_Dispatch(ipc_msg);
	// NX_Printf("vfs: ret:%d\n", ret);
    NX_IpcReturn(ret);
}

int main(int argc, char **argv)
{
    NX_Printf("Hello, Welcome to filesystem!\n");

    NX_Error err;
    if (NX_IpcBind("vfs", VFS_IpcHandler, 32) < 0)
    {
        NX_Printf("VFS bind error!\n");
        return -1;
    }

    NX_FileSystemInit();

    err = VFS_FileSystemMount("sd0", "/", "fat", 0);
    if (err != NX_EOK)
    {
        NX_Printf("Mount sd0 error!, err = %s\n", NX_ErrorToString(err));
        return -1;
    }
    NX_Printf("mount fatfs ok\n");
    err = VFS_FileSystemMount(NX_NULL, "/System/Device", "devfs", 0);
    if (err != NX_EOK)
    {
        NX_Printf("Mount /System/Device error!, err = %s\n", NX_ErrorToString(err));
        return -1;
    }
    NX_Printf("mount devfs ok\n");

    while (1)
    {
        // NX_ThreadYield();
        NX_ThreadSleep(5000);
    }

    return 0;
}